﻿///////////////////////////////////////////////////////////////////////////////
//
// This file is part of the BassDrop project
//   https://code.google.com/p/bass-drop/
//
//
// Creator:	Gergely Krajcsovszki (krajcsovszkig@hotmail.com)
// Date:	2014-04-21 12:30
//
//
// Copyright © 2013-2014. Gergely Krajcsovszki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace BassDrop.Core.Common.Utilities
{
	/// <summary>
	/// Contains various utility methods regarding the creation and manipulation of regexes
	/// </summary>
	public static class RegexUtils
	{
		/// <summary>
		/// Returns a regex that matches a string equal to any of the passed strings
		/// </summary>
		/// <param name="strings"></param>
		/// <returns></returns>
		public static Regex MatchAny( IEnumerable<string> strings )
		{
			return new Regex( string.Join( "|", strings.Select<string, string>( ( item ) =>
			{
				return Regex.Escape( item );
			} ) ) );
		}

		/// <summary>
		/// Returns a regex that matches a string beginning with any of the passed strings
		/// </summary>
		/// <param name="strings"></param>
		/// <returns></returns>
		public static Regex MatchStartWithAny( IEnumerable<string> strings )
		{
			return new Regex( string.Join( "|", strings.Select<string, string>( ( item ) =>
			{
				return MatchStartWith( item ).ToString();
			} ) ) );
		}

		/// <summary>
		/// Returns a regex that matches a string ending with any of the passed strings
		/// </summary>
		/// <param name="strings"></param>
		/// <returns></returns>
		public static Regex MatchEndWithAny( IEnumerable<string> strings )
		{
			return new Regex( string.Join( "|", strings.Select<string, string>( ( item ) =>
			{
				return MatchEndWith( item ).ToString();
			} ) ) );
		}

		/// <summary>
		/// Returns a regex that matches a string containing any of the passed strings
		/// </summary>
		/// <param name="strings"></param>
		/// <returns></returns>
		public static Regex MatchContainsAny( IEnumerable<string> strings )
		{
			return new Regex( string.Join( "|", strings.Select<string, string>( ( item ) =>
			{
				return MatchContains( item ).ToString();
			} ) ) );
		}

		/// <summary>
		/// Returns a regex that matches a string beginning with the passed string
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static Regex MatchStartWith( string str )
		{
			return new Regex( string.Format( "^{0}.*", Regex.Escape( str ) ) );
		}

		/// <summary>
		/// Returns a regex that matches a string ending with the passed string
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static Regex MatchEndWith( string str )
		{
			return new Regex( string.Format( ".*{0}$", Regex.Escape( str ) ) );
		}

		/// <summary>
		/// Returns a regex that matches a string containing the passed string
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static Regex MatchContains( string str )
		{
			return new Regex( string.Format( ".*{0}.*", Regex.Escape( str ) ) );
		}
	}
}
